;; core/compose.scm - (c) rohan drape, 2000-2005

;; Evaluates to a one argument procedure that is a right to left
;; composition of the one argument procedures `a' and `b'.

(define (compose2 a b)     (lambda (v) (a (b v))))
(define (compose3 a b c)   (lambda (v) (a (b (c v)))))
(define (compose4 a b c d) (lambda (v) (a (b (c (d (v)))))))
(define compose compose2)

(define (compose2-left a b)   (lambda (v) (b (a v))))
(define (compose3-left a b c) (lambda (v) (c (b (a v)))))
(define (compose4-left a b c d) (lambda (v) (d (c (b (a v))))))
(define compose-left compose2-left)

;; N-ary variants.

(define (compose-n-left . p)
  (define (f p v) (if (null? p) v (f (cdr p) ((car p) v))))
  (lambda (v) (if (null? p) v (f p v))))

(define (compose-n . p) (apply compose-n-left (reverse p)))
